/**
 * 产品JS类
 * @author 钟华山
 * @from projectView.js
 */


var prdPath ="";

function setPathPrefix(path){
	alert(path);
	prdPath =  path;
}

$(function(){
	//新增业务分类初始化事件
	$("#addPrjBizTypeList").click(function() {
		var prjBizTypeIds = "";
		$('input[name="prjBizType"]:checked').each(function(){
			prjBizTypeIds += $(this).val() + ",";
			}); 
		var actionUrl = prdPath+"addPrjBizType.do?prjBizTypeIds=" + prjBizTypeIds;
		openDialog(actionUrl);
	});

	$("#isAbs").change(function(){
		isAbsToTxt();
	});
	
	worthDisclosureFrequencyAttr();
	
	
	
});

/**
 * 新增业务分类
 */
function addPrjBizType(ids){
	var prjBizTypeIds = ids.split(" ");
	var prjBizTypeDiv = $("#prjBizTypeDiv");
	var html = "";
	var prjBizTypeIdHidden = "";
	var prjBizTypeShow = "";
	for(var i = 0; i < prjBizTypeIds.length; i++){
		if(prjBizTypeIds[i] != ""){
			var prjBizTypeId = prjBizTypeIds[i];
			var prjBizTypeIdAndText = prjBizTypeId.split(",");
			//因选择业务分类时后面的空值没有处理
			prjBizTypeIdHidden += prjBizTypeIdAndText[0] + ",";
			prjBizTypeShow += prjBizTypeIdAndText[1] + " ";
		}
	}
	prjBizTypeDiv.html(prjBizTypeShow);
	$("#prjBizType").val(prjBizTypeIdHidden);
}

var isShow=$(".form-title").text();
//校验是否通过
var isCheckTrue = false;

$(".listenerClass").hover(
	  function () {
		$(".modifyContent").hide();
		//$(this).children("span:nth-child(2)").show();
		$(this).children(".modifyContent").show();
	  },
	  function () {
		  $(".modifyContent").hide();
	  }
);

$(".openProjectUpdateImg").click(function(){

	//禁止滚动条滚动
	$("body").css("overflow-y","hidden");
	var name = $(this).attr("name");
	$("#showRemark").attr("class",name);
	$("#project_btn").attr("class",name);
	
	$("."+name).show();
	$("."+name).addClass("active");
	
	$("#project_update_div_bg").show();
});

function closeProjectUpdate(){
	//解除滚动条滚动
	$("body").css("overflow-y","auto");
	
	$("#project_update_div_bg").hide();
	var values= $("#project_update_div_bg .active:eq(0)").attr("class");
	var column_names=values.split(" ");
	var column_name =column_names[0];
	$("#"+column_name).val("");
	$("[name=remark]").val("");
	$("#project_update_div_bg tr").removeClass("active");
	$("#project_update_div_bg tr").hide();
	
}

		
//系列产品简称重复性校验
function isHasSamePRJSNameView(){
	var prjSName = $("#PRJSNAME1").val();
	var prjCode = $("#prjCode").val();
	if(prjSName == "") {
		return false;
	}
	$.ajax({
		type: "GET",
		dataType: "json",
		async: false, 
		url: prdPath+"/isExistPrjSname.do?prjSName="+encodeURI(prjSName)+"&prjCode="+prjCode,
		success: function(data){
			//大于0就是代表有重复
			if (data.count > 0)  {
				$(".project_update_error font").text("提示：该名称已被使用");
				$(".project_update_error").show();
				isCheckTrue = false;
			}else {
				 $("#prjSNameVerify").hide();
				 isCheckTrue = true;
			 }
		},
		error: function(jqXHR, textStatus, errorThrown){
			ctools.alert("系列产品简称重复性校验出错" + textStatus, "", "warning");
			isCheckTrue = false;
		}
	});
}

//ajax 修改
function projectUpdate(){
	//修改属性的列名
	var values= $("#project_update_div_bg .active:eq(0)").attr("class");
	var column_names=values.split(" ");
	var column_name =column_names[0];
	if(column_name=='PRJSNAME'){
		if(!isCheckTrue){
			var prjSName = $("#PRJSNAME1").val();
			if(prjSName == "") {
				$(".project_update_error font").text("系列产品简称（专户简称）不能为空");
				$(".project_update_error").show();
			}
			return false;
		}
	}
	//错误提示 ， title_text不能为空 ，如title_text=专户名称（全称）
	var title_text = $("#project_update_div_bg .active:eq(0) span").text();
	//修改属性原来的值
	var old_value = $("#project_update_div_bg .active:eq(0) input").val();
	var old_text=$("#project_update_div_bg .active:eq(0) label").text();
	var new_value = "";
	var new_text = "";
	//获取当前标签的名称： INPUT SELECT
	var name=$("#project_update_div_bg .active:eq(1) td").children().last().attr("name");
	if(name=='remark'){
		old_value="";
		new_value = "";
		new_text = "";
		if(column_name=='IS_SPECIAL_TRADE'||column_name=='REPORT_TYPE'){
			old_text=old_text.replace(/(^\s*)/g,"");
			old_text=old_text.replace(/\s+/g,",");
			old_text=old_text.substring(0, old_text.length-1); 
		}else{
			old_text=old_text.replace(/\s+/g,"");
		}
	}else{
		var tagName =$("#project_update_div_bg .active:eq(1) td").children().last().prop("tagName");
		var tagType = "";
		if(tagName=='SELECT'){
			//待测试
			new_value = $("#project_update_div_bg .active:eq(1) td select option:selected").val();//序号
			new_text = $("#project_update_div_bg .active:eq(1) td select option:selected").text();//显示值
			if(new_text==''){
				$(".project_update_error font").text(title_text+"不能为空");
				$(".project_update_error").show();
				return;
			}
			new_text=new_text.replace(/\s+/g,"");
			old_text=old_text.replace(/\s+/g,"");
		}else if(tagName=='INPUT'){
			//input类型：获取当前标签的type
			tagType = $("#project_update_div_bg .active:eq(1) td").children().last().attr("type");
			
			if(tagType == 'text' || tagType == 'hidden'){
				new_value = $("#project_update_div_bg .active:eq(1) td").children().last().val();
				new_text=$("#project_update_div_bg .active:eq(1) td").children().last().val();
				old_text=old_text.replace(/(^\s*)/g,"");
				old_text=old_text.replace(/\s+/g,",");
				old_text=old_text.substring(0, old_text.length-1);
			}else if(tagType == 'checkbox'){
				
				$("#project_update_div_bg .active:eq(1) td input[name='"+column_name+"']:checked").each(function(){
					new_value+=$(this).val()+",";
					new_text+=$(this).attr("textVal")+",";
				});
				if(new_value!="" && new_value!=null){
					new_value = new_value.substring(0, new_value.length-1); 
					new_text = new_text.substring(0, new_text.length-1); 
				}
				new_text=new_text.replace(/\s+/g,"");
				old_text=old_text.replace(/(^\s*)/g,"");
				old_text=old_text.replace(/\s+/g,",");
				old_text=old_text.substring(0, old_text.length-1); 
			}else{
				ctools.alert("error: other input type!", "", "warning");
			}
			
		}else if(tagName=='TEXTAREA'){
			new_value = $("#project_update_div_bg .active:eq(1)").children().last().text();
		}else{
			ctools.alert("error: other tag!" , "", "warning");
		}
		//当修改的属性为输入框时，做为空判断
		if(new_value==''){
			$(".project_update_error font").text(title_text+"不能为空");
			$(".project_update_error").show();
			return;
		}
	}
	title_text=title_text.replace(/\s+/g,"");
	var prjCode = $("#prjCode").val();
	var remark=$("#remark").val();
	if(remark.length>500){
		ctools.alert("备注不能超过500个字符", "", "warning");
		return;
	}
	var viewSource=$("#viewSource").val();
	var params = {	"code": prjCode,
					"columnName": column_name,
					"oldValue":old_value,
					"newValue":new_value,
					"remark":remark,
					"viewSource":viewSource,
					"newText":new_text,
					"oldText":old_text,
					"modifyColumnName":title_text
			};
	var url = '/cbp/product/prepare/productManage/updateTableSingleAttr.do';
	$.ajax({
		//删除附件与系列产品关系表
		url: url,   
		dataType: "json",
		type: "POST",
		data: params,
		cache: false,
		async: false,
		error: function(jqXHR, textStatus, errorThrown){
				ctools.alert("修改失败" + textStatus, "", "warning");
		},
		success: function(data) {	 
			ctools.alert_sweet("修改成功！", "success", "",function(){
				window.location.reload();
			});
		}
	});	
}

//信息披露操作
function viewReportTypeOperate(){
	//如果选择无，就清空其他选项
	if(document.getElementById("reportType4").checked){
		$("input[name='REPORT_TYPE']").prop("checked",false);
		$("input[name='REPORT_TYPE']").prop("disabled",true);
		$("#reportType4").prop("checked",true);
		$("#reportType4").removeAttr("disabled");
	}else{
		$("input[name='REPORT_TYPE']").removeAttr("disabled");
	}
}

//复核通过
function pass(){
	$("form[name='prjCheckForm']").action = prdPath + "passOrRetProject.do?method=pass";
	SubmitAndPreventSecond("prjCheckForm",null,true);
}

//复核驳回
function ret(){
	document.prjCheckForm.action = prdPath + "/passOrRetProject.do?method=reject";
	SubmitAndPreventSecond("prjCheckForm",null,true);
}

/**
 * 要素变更-净值披露频率相关字段显示
 */
function worthDisclosureFrequencyAttrForChange(worthDisclosureFrequency) {
	/*1:每一个交易日
	2:每周最后一个交易日
	3:每月最后一个交易日
	4:每季最后一个交易日
	5:每周
	6:每月
	8:每季
	9:每半年
	10:每年
	11:其他
	12:无*/
	
	// isDiscWorthEndMonthTr: 是否披露月末净值
	// worthDisclosureTimeTr：净值披露时间（T+N）
	// worthDisclosureTitTd：净值日期 title
	// worthDisclosureValTd：净值日期 value
	// worthDisclosureWeekDiv：周
	// isDiscWorthEndMonthTr：月
	// worthDisclosureDayDiv：日
	hideTimeSpan();
	if( "2" == worthDisclosureFrequency){
		$("#isDiscWorthEndMonthTr").show();
		$("#worthDisclosureTimeTr").show();
	} else if ("1" == worthDisclosureFrequency || "3" == worthDisclosureFrequency 
			|| "4" == worthDisclosureFrequency || "11" == worthDisclosureFrequency) {
		$("#worthDisclosureTimeTr").show();
	} else if ("5" == worthDisclosureFrequency) {
		$("#worthDisclosureTitTd").show();
		$("#worthDisclosureValTd").show();
		$("#worthDisclosureWeekDiv").show();
		$("#isDiscWorthEndMonthTr").show();
		$("#worthDisclosureTimeTr").show();
		// 显示 周
		$("#WORTH_DISCLOSURE_FREQUENCY_MONTH").show();
	} else if ("6" == worthDisclosureFrequency) {
		$("#worthDisclosureTitTd").show();
		$("#worthDisclosureValTd").show();
		$("#worthDisclosureMonthDiv").show();
		$("#worthDisclosureDayDiv").show();
		$("#isDiscWorthEndMonthTr").show();
		$("#worthDisclosureTimeTr").show();
		// 显示 日
		$("#WORTH_DISCLOSURE_FREQUENCY_DAY").show();
	} else if ("8" == worthDisclosureFrequency || "9" == worthDisclosureFrequency || "10" == worthDisclosureFrequency) {
		$("#worthDisclosureTitTd").show();
		$("#worthDisclosureValTd").show();
		$("#worthDisclosureMonthDiv").show();
		$("#worthDisclosureDayDiv").show();
		$("#worthDisclosureTimeTr").show();
		//显示 交易日  月
		$("#WORTH_DISCLOSURE_FREQUENCY_MONTH").show();
		$("#WORTH_DISCLOSURE_FREQUENCY_DAY").show();

	}
}

/**
 * 隐藏
 */
function hideTimeSpan(){
//	debugger;
	$("#WORTH_DISCLOSURE_FREQUENCY_MONTH").hide();
	//console.log($("#WORTH_DISCLOSURE_FREQUENCY_WEEK"));
	$("#WORTH_DISCLOSURE_FREQUENCY_WEEK").hide();
	//console.log($("#WORTH_DISCLOSURE_FREQUENCY_DAY"));
	$("#WORTH_DISCLOSURE_FREQUENCY_DAY").removeClass("show");
	$("#WORTH_DISCLOSURE_FREQUENCY_DAY").hide();
}

/**
 * 净值披露频率
 */
function worthDisclosureFrequencyAttr() {
	var worthDisclosureFrequency = $("#worthDisclosureFrequency").val();
	if( "2" == worthDisclosureFrequency){
		$("#isDiscWorthEndMonthTr").show();
		$("#worthDisclosureTitTd").hide();
		$("#worthDisclosureValTd").hide();
		$("#worthDisclosureTimeTr").show();
	} else if ("1" == worthDisclosureFrequency || "3" == worthDisclosureFrequency 
			|| "4" == worthDisclosureFrequency || "11" == worthDisclosureFrequency) {
		$("#worthDisclosureTitTd").hide();
		$("#worthDisclosureValTd").hide();
		$("#isDiscWorthEndMonthTr").hide();
		$("#worthDisclosureTimeTr").show();
	} else if ("5" == worthDisclosureFrequency) {
		$("#worthDisclosureTitTd").show();
		$("#worthDisclosureValTd").show();
		$("#worthDisclosureWeekDiv").show();
		$("#worthDisclosureMonthDiv").hide();
		$("#worthDisclosureDayDiv").hide();
		$("#isDiscWorthEndMonthTr").show();
		$("#worthDisclosureTimeTr").show();
	} else if ("6" == worthDisclosureFrequency) {
		$("#worthDisclosureTitTd").show();
		$("#worthDisclosureValTd").show();
		$("#worthDisclosureWeekDiv").hide();
		$("#worthDisclosureMonthDiv").show();
		$("#worthDisclosureDayDiv").show();
		$("#isDiscWorthEndMonthTr").show();
		$("#worthDisclosureTimeTr").show();
	} else if ("7" == worthDisclosureFrequency) {
		$("#worthDisclosureTitTd").show();
		$("#worthDisclosureValTd").show();
		$("#worthDisclosureWeekDiv").hide();
		$("#worthDisclosureMonthDiv").show();
		$("#worthDisclosureDayDiv").show();
		$("#isDiscWorthEndMonthTr").show();
		$("#worthDisclosureTimeTr").show();
	} else if ("8" == worthDisclosureFrequency || "9" == worthDisclosureFrequency || "10" == worthDisclosureFrequency) {
		$("#worthDisclosureTitTd").show();
		$("#worthDisclosureValTd").show();
		$("#worthDisclosureWeekDiv").hide();
		$("#worthDisclosureMonthDiv").show();
		$("#worthDisclosureDayDiv").show();
		$("#isDiscWorthEndMonthTr").hide();
		$("#worthDisclosureTimeTr").show();
	}else if("12" == worthDisclosureFrequency){
		$("#worthDisclosureTitTd").hide();
		$("#worthDisclosureValTd").hide();
		$("#isDiscWorthEndMonthTr").hide();
		$("#isDiscWorthEndMonthTr").hide();
		$("#worthDisclosureTimeTr").hide();
	} else {
		$("#worthDisclosureTitTd").hide();
		$("#worthDisclosureValTd").hide();
		$("#isDiscWorthEndMonthTr").hide();
	}
}